package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.AssociatedStoresDao;
import com.hs.panghu.entity.Community;
import com.hs.panghu.entity.CourierStation;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AssociatedStoresDaoImpl implements AssociatedStoresDao {



    @Override
    public List<Shop> selectAssociatedStores(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        //动态sql和动态数组
        StringBuilder sb = new StringBuilder("SELECT shop.* from shop,communityshop WHERE shop.shopId=? ");


        List param = new ArrayList();
        String communityId = map.get("communityId");
        param.add(communityId);

        //判断communityName是否有数据，如果有，则拼接
        String shopName = map.get("shopName");
//        System.out.println("111" + shopName);
        if (shopName != null && !"".equals(shopName)) {//axios的发送的路径参数没有时为null
            sb.append(" and shopName LIKE ? ");
            param.add("%" + shopName + "%");
        }
        //communityId
//        String communityId = map.get("communityId");
//        sb.append(" and communityId = ? ");
//        param.add(communityId);


        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sb.append(" order by shopId desc limit ?,? ");
        param.add((pageNum - 1) * pageSize);
        param.add(pageSize);


        String sql = sb.toString();
        System.out.println(sql);

        Object[] object = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()) {
                Shop shop = new Shop(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getString(11),
                        resultSet.getInt(12),
                        resultSet.getDate(13),
                        resultSet.getInt(14),
                        resultSet.getString(15),
                        resultSet.getString(16),
                        resultSet.getString(17),
                        resultSet.getInt(18),
                        resultSet.getString(19),
                        resultSet.getString(20),
                        resultSet.getString(21),
                        resultSet.getInt(22),
                        resultSet.getString(23),
                        resultSet.getString(24)
                );
                list.add(shop);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectAssociatedStoresCount(Map<String, String> map) {
        int count=0;
        List<Shop> list = new ArrayList<>();
        //动态sql和动态数组
        StringBuilder sb = new StringBuilder("SELECT count(1) from shop,communityshop WHERE shop.shopId=? ");
        List param = new ArrayList();
        String communityId = map.get("communityId");
        param.add(communityId);

        //判断communityName是否有数据，如果有，则拼接
        String shopName = map.get("shopName");
//        System.out.println("111" + shopName);
        if (shopName != null && !"".equals(shopName)) {//axios的发送的路径参数没有时为null
            sb.append(" and shopName LIKE ? ");
            param.add("%" + shopName + "%");
        }

//        //communityId
//        String communityId = map.get("communityId");
//        sb.append(" and communityId = ? ");
//        param.add(communityId);



        String sql = sb.toString();
        System.out.println(sql);

        Object[] object = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
                System.out.println("条数"+count);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
